﻿package view.components 
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	/**
	* ...
	* @author Slavshik Alexander
	*/
	public class BaseImage extends Sprite {
		protected var _rloader:MovieClip = new RoundLoader();
		protected var _image:Loader = new Loader();
		protected var _width:Number;
		protected var _height:Number;
		private var __content:Bitmap = new Bitmap();
		private var __noImage:NoPhoto = new NoPhoto();
		private var __fit:Boolean;
		
		public function BaseImage(w:Number = 50, h:Number = 50, fit:Boolean = false) {
			__fit = fit;
			_width = w;
			_height = h;
			focusRect = false;
			addChild(_rloader);
			_rloader.x = _width * 0.5
			_rloader.y = _height * 0.5
			_rloader.stop();
			addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
		}
		public function load(src:String):void {
			var urlRequest:URLRequest = new URLRequest(src);
			destroy();
			_image = null;
			_image = new Loader();
			if (contains(__content)) removeChild(__content);
			addChild(_rloader);
			_rloader.gotoAndStop(1);
			try {
				_image.load(urlRequest, new LoaderContext(true));
			}catch (e:Error) { onLoadFailed(null); }
			_image.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoadFailed, false, 0, true);
			_image.contentLoaderInfo.addEventListener(Event.INIT, onLoadInit, false, 0, true);
			_image.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadFailed, false, 0, true);
			_image.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoadProgress, false, 0, true);
		}
		private function onSecurityError(e:SecurityErrorEvent):void {
			if (contains(__noImage)) removeChild(__noImage);
		}
		protected function onLoadInit(e:Event):void {
			try {
				__content.bitmapData = (_image.contentLoaderInfo.content as Bitmap).bitmapData.clone();
				__content.smoothing = true;
				addChild(__content);
				loadComplete(e);
			}catch (err:Error) {
				onLoadFailed(null);
			}
		}
		protected function onLoadProgress(e:ProgressEvent):void {
			if (e.bytesLoaded <= 0) return;
			_rloader.gotoAndStop(int(e.bytesLoaded / e.bytesTotal * 100));
		}
		protected function onUnloaded(e:Event):void {
			_image = null;
			__content.bitmapData.dispose();
		}
		public function destroy():void {
			try { _image.close(); } catch (e:Error) { }
			try {
				if(_image.content != null){
					if (_image.contentLoaderInfo.childAllowsParent && _image.contentLoaderInfo.content is Bitmap) {
						(_image.contentLoaderInfo.content as Bitmap).bitmapData.dispose();
					}
				}
			}catch(e:Error){}
			_image.contentLoaderInfo.removeEventListener(Event.INIT, onLoadInit);
			_image.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onLoadFailed);
			_image.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onLoadProgress);
			_image.unload();
		}
		public function place():void {
			__content.width = _width;
			__content.height = _height;
			__content.scaleX = __content.scaleY = Math.min(__content.scaleX, __content.scaleY);
			__content.x = (_width - __content.width) * 0.5;
			__content.y = (_height - __content.height) * 0.5;
		}
		protected function onLoadFailed(e:IOErrorEvent):void {
			if (contains(_rloader)) removeChild(_rloader);
			addChild(__noImage);
		}
		protected function loadComplete(e:Event = null):void {
			if (contains(_rloader)) removeChild(_rloader);
			if (__fit) place();
			destroy();
			dispatchEvent(e);
		}
		protected function onRemoved(e:Event):void {
			destroy();
		}
		public function get content():Bitmap {
			return __content;
		}
		
	}
	
}